#include "COptionsState.h"
#include "CMainMenuState.h"
#include "CGame.h"

#include "SGD Wrappers/CSGD_DirectInput.h"

COptionsState::COptionsState(void)
{
	m_nIndex = 0;
	m_nBGMVol = 60;
	m_nSFXVol = 100;

	m_nCursorID = -1;
	m_nBGMID = -1;
	m_nSFXID = -1;

	m_BMFonts.SetImageID(CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/AnG_bitmapfont.png", D3DXCOLOR(0,255,0,255)));
	m_BMFonts.SetCharWidth(8);
	m_BMFonts.SetCharHeight(9);
	m_BMFonts.SetCol(16);
	m_BMFonts.SetStartChar('@');
}
COptionsState::~COptionsState(void)
{

}

COptionsState* COptionsState::GetInstance(void)
{
	static COptionsState instance;
	return &instance;
}
void COptionsState::Enter()
{
	m_pD3D	=	CSGD_Direct3D::GetInstance();
	m_pTM	=	CSGD_TextureManager::GetInstance();
	m_pDS	=	CSGD_DirectSound::GetInstance();
	m_pWM	=	CSGD_WaveManager::GetInstance();

	m_nBGMID = m_pWM->LoadWave("resources/sound files/BGM.wav");
	m_pWM->Play(m_nBGMID);
	m_pWM->SetVolume(m_nBGMID, m_nBGMVol);

	m_nSFXID = m_pWM->LoadWave("resources/sound files/MegamanExplode.wav");
	m_pWM->SetVolume(m_nSFXID, m_nSFXVol);
	

	m_nCursorID = m_pTM->LoadTexture("resources/graphics/SGD_MenuCursor.png", D3DCOLOR_XRGB(255, 255, 255));

}
bool COptionsState::Input()
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	if(pDI->KeyPressed(DIK_UPARROW))
	{
		if(--m_nIndex < 0)
			m_nIndex = 2;
	}
	else if(pDI->KeyPressed(DIK_DOWNARROW))
		if(++m_nIndex > 2)
			m_nIndex = 0;

	if(pDI->KeyPressed(DIK_RETURN))
	{
		if(m_nIndex == 2)
			CGame::GetInstance()->ChangeState(CMainMenuState::GetInstance());
		else if(m_nIndex == 1)
			m_pWM->Play(m_nSFXID);
	}

	if(pDI->KeyDown(DIK_RIGHT) && m_nIndex == 0 && m_nBGMVol < 100)
	{
		++m_nBGMVol;
		m_pWM->SetVolume(m_nBGMID, m_nBGMVol);
	}
	if(pDI->KeyDown(DIK_LEFT) && m_nIndex == 0 && m_nBGMVol > 0)
	{
		--m_nBGMVol;
		m_pWM->SetVolume(m_nBGMID, m_nBGMVol);
	}
	if(pDI->KeyDown(DIK_RIGHT) && m_nIndex == 1 && m_nSFXVol < 100)
	{
		++m_nSFXVol;
		m_pWM->SetVolume(m_nSFXID, m_nSFXVol);
	}
	if(pDI->KeyDown(DIK_LEFT) && m_nIndex == 1 && m_nSFXVol > 0)
	{
		--m_nSFXVol;
		m_pWM->SetVolume(m_nSFXID, m_nSFXVol);
	}

	return false;
}
void COptionsState::Update(float fElapsedTime)
{
}
void COptionsState::Render(void)
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();

	pD3D->Clear(0, 0, 0);
	pD3D->DeviceBegin();

	pD3D->SpriteBegin();

	CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 80,100 + (m_nIndex * 50)); //, 4.0f, 4.0f, NULL, 64.0f, 65.0f, 0, D3DCOLOR_ARGB(128, 255, 0, 0));
	m_BMFonts.Draw("BGM VOLUME", 100,100, 1.0f, D3DXCOLOR(255,255,255,255));
	m_BMFonts.Draw("SFX VOLUME", 100,150, 1.0f, D3DXCOLOR(255,255,255,255));
	m_BMFonts.Draw("EXIT", 100,200, 1.0f, D3DXCOLOR(255,255,255,255));



	pD3D->SpriteEnd();

	pD3D->DeviceEnd();
	//pD3D->DrawText("BGM Volume", 100, 100);
	
	char buffer1[255];
	sprintf_s(buffer1, sizeof(buffer1), "%d", m_nBGMVol);
	pD3D->DrawText(buffer1,200, 100);

	//pD3D->DrawText("SFX Volume", 100, 150);

	char buffer2[255];
	sprintf_s(buffer2, sizeof(buffer2), "%d", m_nSFXVol);
	pD3D->DrawText(buffer2,200, 150);

	//pD3D->DrawText("Exit",100,200);

	pD3D->Present();
}
void COptionsState::Exit(void)
{
	m_pWM->UnloadWave(m_nBGMID);
}